package com.libui.webview.jsinterface.handlers

import com.libbase.cast
import com.libbase.utils.ThreadUtils
import com.libui.LibUI
import com.libui.webview.WebViewEx
import com.libui.webview.jsinterface.WebViewHandlerInterface

class WebViewMessageHandler : WebViewHandlerInterface {
    companion object {
        const val id: String = "postWebViewMessage"
    }

    override val id: String
        get() {
            return Companion.id
        }

    override fun handle(webView: WebViewEx?, params: Map<String, Any>?) {
        if (webView == null) {
            return
        }

        if (params == null) {
            return
        }

        val messageId: String = params["messageId"] as? String ?: return
        val data = cast<Map<String, Any>>(params["data"])

        ThreadUtils.instance.executeInMainThread {
            LibUI.instance.adapter?.onWebViewMessage(messageId,data)
        }

    }
}